<?php
declare(strict_types=1);
namespace app\admin\controller;
class History extends Cp
{
    protected string $model = 'doc_version';
    protected string $order = 'id DESC';

    protected function _where(): array
    {
        $where = [];
        $doc_id = input('doc_id', 0, 'intval');
        if ($doc_id > 0) {
            $where['doc_id'] = $doc_id;
        }
        return $where;
    }

    // 查看
    public function detail(int $id)
    {
        $model = model($this->model)->find($id);
        if (!$model) {
            $this->error('记录不存在');
        }
        return view()->with('vo', $model->toArray());
    }

    // 恢复
    public function restore(int $id)
    {
        if ($this->isAjax()) {
            $history = db($this->model)->field('id,doc_id,book_id,markdown,user_id,ver_int')->where('id', $id)->find();
            if (!$history) {
                $this->error('原文档已不存在');
            }
            $data = [];
            $data['markdown'] = $history['markdown'];
            $data['ver_int'] = $history['ver_int'];
            $data['user_id'] = $history['user_id'];
            $r = db('doc')->where('id', $history['doc_id'])->update($data);
            widget_reload('doc');
            $this->_jump(['恢复成功', '恢复失败'], $r, 'index');

        }
        $this->error('非法操作');
    }
}